﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;


public partial class AdminX_Tour_TourPopularAdd : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLabelLanguage();
            ValidateControl();
            BindDataPriorityDropdown();
            BindDataToLanguageDropDown();
            BindDataWhenEdit();
        }
    }
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();

        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblDescription.Text = MultipleLanguage.LBL_TOURPOPULAR_DESCRIPTION[nDefaultLanguage - 1];
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
        chkDisplayOrder.Text = MultipleLanguage.lbl_DesplayOrder[nDefaultLanguage - 1];
        chkIsHomePage.Text = MultipleLanguage.LBL_ISHOMEPAGE[nDefaultLanguage - 1];
    }
    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataWhenEdit();
    }
    #endregion

    #region Relative Functions
    private void ValidateControl()
    {
        rfvDescription.ErrorMessage = MessagesValidate.TOURPOPULAR_EMPTY[Support.GetDefaultLanguage() - 1];
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    TourPopular objTourPopular = new TourPopular();
                    objTourPopular.LoadById(nID);
                    int nDisplayOrder = 0;
                    nDisplayOrder = ConvertData.ConvertToInt(objTourPopular.Data.Priority);
                    if (nDisplayOrder > 0)
                        chkDisplayOrder.Checked = true;
                    else
                        chkDisplayOrder.Checked = false;

                    if (ConvertData.ConvertToInt(objTourPopular.Data.IsHomePage) > 0)
                        chkIsHomePage.Checked = true;
                    else
                        chkIsHomePage.Checked = false;

                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtDescription.Text = ConvertData.ConvertToString(objTourPopular.Data.Description1);
                            break;
                        case 2:
                            txtDescription.Text = ConvertData.ConvertToString(objTourPopular.Data.Description2);
                            break;
                        case 3:
                            txtDescription.Text = ConvertData.ConvertToString(objTourPopular.Data.Description3);
                            break;
                    }
                }
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURPOPULAR;
            Response.Redirect(sLink);
        }
    }
    private eTourPopular SetData()
    {
        TourPopular objTourPopular = new TourPopular();
        eTourPopular eTourPopular = this.TourPopularEntity();
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        switch (nCurrentLanguage)
        {
            case 1:
                objTourPopular.Data.Description1 = ConvertData.ConvertToString(txtDescription.Text.Trim());

                objTourPopular.Data.Description2 = ConvertData.ConvertToString(eTourPopular.Description2);
                objTourPopular.Data.Description3 = ConvertData.ConvertToString(eTourPopular.Description3);
                break;
            case 2:
                objTourPopular.Data.Description2 = ConvertData.ConvertToString(txtDescription.Text.Trim());

                objTourPopular.Data.Description1 = ConvertData.ConvertToString(eTourPopular.Description1);
                objTourPopular.Data.Description3 = ConvertData.ConvertToString(eTourPopular.Description3);
                break;
            case 3:
                objTourPopular.Data.Description3 = ConvertData.ConvertToString(txtDescription.Text.Trim());

                objTourPopular.Data.Description1 = ConvertData.ConvertToString(eTourPopular.Description1);
                objTourPopular.Data.Description2 = ConvertData.ConvertToString(eTourPopular.Description2);
                break;
        }
        if (chkDisplayOrder.Checked) objTourPopular.Data.Priority = ConvertData.ConvertToInt(ddlPriority.SelectedValue);
        else objTourPopular.Data.Priority = 0;

        if (chkIsHomePage.Checked)
            objTourPopular.Data.IsHomePage = 1;
        else
            objTourPopular.Data.IsHomePage = 0;

        return objTourPopular.Data;
    }
    private void BindDataPriorityDropdown()
    {
        DataTable dtbTourPopular = new DataTable();
        TourPopular objTour = new TourPopular();

        dtbTourPopular = objTour.GetAll();
        int nCount = dtbTourPopular.Rows.Count;
        string sAction = GetQuerryString(Config.ACTION);//Lay Thong tin Edit, Insert
        int nRealValue = 0;
        if (sAction == Config.ACTION_EDIT)//Kiem tra dieu kien
        {
            eTourPopular eTourPopularEntity = this.TourEntity();
            
            if (ConvertData.ConvertToInt(eTourPopularEntity.Priority) < nCount)
            {
                nRealValue = nCount;
            }
            else
            {
                nRealValue = ConvertData.ConvertToInt(eTourPopularEntity.Priority);
            }
            for (int i = 0; i < nRealValue; i++)
            {
                ddlPriority.Items.Add(ConvertData.ConvertToString(i + 1));
            }

            string sPriority = ConvertData.ConvertToString(eTourPopularEntity.Priority);
            ddlPriority.SelectedValue = sPriority;
        }
        else
        {
            nRealValue = nCount + 1;
            for (int i = 0; i < nRealValue; i++)
            {
                ddlPriority.Items.Add(ConvertData.ConvertToString(i + 1));
            }
            ddlPriority.SelectedValue = ConvertData.ConvertToString(nRealValue);
        }
    }
    private eTourPopular TourEntity()
    {
        eTourPopular entityeTour = new eTourPopular();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            TourPopular objTour = new TourPopular();
            objTour.LoadById(nItemID);
            entityeTour = objTour.Data;
        }
        return entityeTour;
    }
    private string GetQuerryString(string sQuerry)
    {
        string sQuerryString = "";
        sQuerryString = ConvertData.ConvertToString(Request.QueryString[sQuerry]);
        return sQuerryString;
    }
    private eTourPopular TourPopularEntity()
    {
        eTourPopular entityTourPopular = new eTourPopular();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            TourPopular objTourPopular = new TourPopular();
            objTourPopular.LoadById(nItemID);
            entityTourPopular = objTourPopular.Data;
        }
        return entityTourPopular;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        TourPopular objTourPopular = new TourPopular();

        objTourPopular.Data = this.SetData();
        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objTourPopular.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURPOPULAR + "&" + Config.MESS_ID + "=1";
                    Response.Redirect(sLink);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objTourPopular.Update(nID);
                if (nResult > 0)
                {
                    Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURPOPULAR);
    }
    #endregion
}